#include "ProductTestMain.h"
#include "InSdkDevInfo.h"

#define DEF_TAG             "TUTKMain"

#define DEF_COM_CLEAN_SKIP_TEST_FLAG       "clean_skip_test_flag"

Com_u8 g_ComThread_Running = DEF_COM_TRUE;
Com_u8 g_ProductTest_Media_Running = DEF_COM_TRUE;
Com_s32 g_ProductTest_ThreadNum = 0;
Com_s32 g_Tutk_ucheck_flag = DEF_COM_FALSE;
StSdkRingBufInfoProducer av_client;

char g_TUTKSdk_UID[DEF_COM_UUID_LEN];

pStSdkRingBufInfoProducer ProductTest_Main_GetRingBufProducer(void)
{
    return &av_client;
}

#if CMAKE_SUPPORT_STEP_MOTOR
Com_pvoid ProductTest_StepMotor_Thread(Com_pvoid arg)
{
    Ext_SdkMotor_Init();
    return NULL;
}

Com_void ProductTest_StepMotor_Init(Com_void)
{
    pthread_t id;

    pthread_create(&id,NULL,ProductTest_StepMotor_Thread,NULL);
    pthread_detach(id);
}
#endif

Com_s32 ProductTest_CheckSSID_CleanFlag(Com_void)
{
    Com_s32 len;
    Com_s8 src[64];
    Com_s8 str[128];
    FILE *fp;

    memset(src,0,sizeof(src));
    memset(str,0,sizeof(str));

    sprintf(str,"iwlist wlan0 scan |grep %s",DEF_COM_CLEAN_SKIP_TEST_FLAG);
    fp = popen(str,"r");
    if(!fp)
    {
        perror("popen iwlist fp");
        return -1;
    }

    fgets(src,sizeof(src),fp);
    len = strlen(src);
    src[len - 1] = '\0';
    //PRT_INFO(DEF_TAG,"wifi: %s\n",src);
    pclose(fp);

    if(strstr(src,DEF_COM_CLEAN_SKIP_TEST_FLAG) != NULL)
    {
        PRT_INFO(DEF_TAG,"clean product test flag\n");
        Ext_InComFile_JudgeRemove(DEF_FLAG_SKIP_HARDWARE_TEST);
    }

    return 0;
}

Com_void ProductTest_CheckSkip(Com_void)
{
    if (Ext_InComFile_Access(DEF_FLAG_SKIP_HARDWARE_TEST) == DEF_COM_FILE_EXIST ||
        Ext_InComFile_Access(DEF_FLAG_SKIP_HARDWARE_TEST_SD) == DEF_COM_FILE_EXIST)
    {
        PRT_INFO(DEF_TAG,"skip hardware test now\n");
        Ext_InSdkTcpSystem_Cmd("skip_product_test.sh");
        sleep(10);
    }
}

Com_void ProductTest_GetInfo(Com_void)
{
    Com_s32 ret;

    memset(g_TUTKSdk_UID,0,sizeof(g_TUTKSdk_UID));

    /* get device UUID */
    ret = Ext_InSdkGetInfo_UUID(g_TUTKSdk_UID);
    if(ret != DEF_COM_SUCCESS)
    {
        strcpy(g_TUTKSdk_UID,DEF_DEFAULT_UUID);
        PRT_ERR(DEF_TAG,"get device uuid failed\n");
    }

    if(Ext_InComFile_Access(DEF_INI_IOT_CONFIG) == DEF_COM_FILE_NOT_EXIST)
    {
        strcpy(g_TUTKSdk_UID,"12345678901234567890");
    }

    PRT_INFO(DEF_TAG,"uid: %s\n",g_TUTKSdk_UID);
}

int main(void)
{
    g_ProductTest_Media_Running = DEF_COM_TRUE;

    /** clear up memory **/
    Ext_InSdkTcpSystem_Cmd("rm /tmp/tutk_test.tar");
    ProductTest_CheckSSID_CleanFlag();
    ProductTest_CheckSkip();

    ProductTest_GetInfo();
    Ext_InComFile_JudgeRemove(DEF_FILE_SAVE_CONF);
    Ext_InComFile_JudgeCreat(DEF_FLAG_NO_NETWORK);
    Ext_InSdkIniCfg_Init();
    Ext_SdkGpio_Init();
    Ext_ProductTest_IRCut_Switch_Init();

#if CMAKE_SUPPORT_STEP_MOTOR
    ProductTest_StepMotor_Init();
#endif

#if CMAKE_SUPPORT_STEEP_ENGINE
    Ext_InPerSteerEngine_Init();
#endif

    Ext_ProductTest_Uart_Init();
    Ext_ProductTest_Key_Init();
    Ext_ProductTest_Led_Init();
    Ext_InSdk_RingBuf_Create_AV_Info(&av_client);
    Ext_ProductTest_Media_Init(&av_client);
    ProductTest_P2P_Main(&av_client);

    return 0;
}
